package com.imay.live.activity.camera;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.imay.capturefilter.utils.ICCons;
import com.imay.capturefilter.utils.ICUtils;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.utils.ToastUtil;
import com.imay.capturefilter.widget.IMCameraView ;
import com.imay.live.R;

/**
 * Created by chan on 2017/6/22 0022.
 */

public class CameraActivity2 extends FragmentActivity implements IMCameraView.IMCameraViewListener {

    private IMCameraView imCameraView;
    private Context mContext;
    private int type;

    private final int SYS = 1;
    private final int PHOTO = 2;

    public static void startActivity(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, CameraActivity2.class);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.capturefilter_activity_camera2);

        Point screenSize = new Point();
        getWindowManager().getDefaultDisplay().getSize(screenSize);
        // 获取屏幕宽度
        ICCons.screenW = screenSize.x;
        ICCons.screenH = screenSize.y;

        mContext = this;

        imCameraView = (IMCameraView) findViewById(R.id.icv_camera);
        imCameraView.setListener(this);

        MobclickAgent.onEvent(mContext, "photo_open");
    }

    @Override
    public void close() {
        finish();
    }

    @Override
    public void takeStart() {
        DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager(), getString(R.string.IC_img_msg_create));
        MobclickAgent.onEvent(mContext, "photo_click_taken");
    }

    @Override
    public void takeComplete(String path) {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
        type = PHOTO;
        MediaScannerConnection.scanFile(mContext,
                new String[]{path}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(final String path, final Uri uri) {
                        // 返回图片路径
                        Intent intent = new Intent(CameraActivity2.this, ClipActivity.class);
                        intent.putExtra(ICCons.KEY_FILE_PATH, path);
                        startActivityForResult(intent, ICCons.REQUEST_CLIP_IMAGE);
                    }
                });
    }

    @Override
    public void takeError() {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
        ToastUtil.show(mContext,R.string.IC_img_msg_error);
    }

    @Override
    public void photopicker() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, ICCons.REQUEST_PICK_IMAGE);
        MobclickAgent.onEvent(mContext, "phote_click_album");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == RESULT_OK) {
                if (requestCode == ICCons.REQUEST_PICK_IMAGE) {    //系统相册返回
                    if (data != null) {
                        type = SYS;
                        Intent intent = new Intent(mContext, ClipActivity.class);
                        intent.putExtra(ICCons.KEY_FILE_URI, data.getData().toString());
                        startActivityForResult(intent, ICCons.REQUEST_CLIP_IMAGE);
                    }
                } else if (requestCode == ICCons.REQUEST_CLIP_IMAGE) {  //图片裁剪返回
                    if (data != null) {
                        Bundle b = data.getExtras();
                        String path = b.getString(ICCons.KEY_FILE_PATH);
                        if(type == PHOTO){
                            ICUtils.deleteFile(mContext, path);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        imCameraView.resumeCamera();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        imCameraView.pauceCamera();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        imCameraView.release();
        super.onDestroy();
    }
}
